<template>
  <div class="app-container">

    <list-page-container>

      <!--      <template slot="query">-->
      <!--        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="90px">-->
      <!--          <el-form-item label="农户姓名" prop="name">-->
      <!--            <el-input-->
      <!--                v-model="queryParams.name"-->
      <!--                placeholder="请输入农户姓名"-->
      <!--                clearable-->
      <!--                style="width: 240px"-->
      <!--                @keyup.enter.native="handleQuery"-->
      <!--            />-->
      <!--          </el-form-item>-->
      <!--          <el-form-item>-->
      <!--            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>-->
      <!--            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>-->
      <!--          </el-form-item>-->
      <!--        </el-form>-->
      <!--      </template>-->

      <template slot="handel">
<!--        <el-row class="handle-container" :gutter="10">-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="primary"-->
<!--                plain-->
<!--                icon="el-icon-plus"-->
<!--                size="mini"-->
<!--                @click="handleAdd"-->
<!--            >新增-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="danger"-->
<!--                plain-->
<!--                icon="el-icon-delete"-->
<!--                size="mini"-->
<!--                :disabled="multiple"-->
<!--                @click="handleDelete"-->
<!--            >删除-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--          <el-col :span="1.5">-->
<!--            <el-button-->
<!--                type="success"-->
<!--                plain-->
<!--                icon="el-icon-upload2"-->
<!--                size="mini"-->
<!--                @click="openImport"-->
<!--            >导入-->
<!--            </el-button>-->
<!--          </el-col>-->
<!--                    <el-col :span="1.5">-->
<!--                      <el-button-->
<!--                          type="warning"-->
<!--                          plain-->
<!--                          icon="el-icon-download"-->
<!--                          size="mini"-->
<!--                          @click="handleExport"-->
<!--                      >导出-->
<!--                      </el-button>-->
<!--                    </el-col>-->
<!--        </el-row>-->
      </template>

      <template slot="table">
        <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
<!--          <el-table-column type="selection" width="55" align="center"/>-->
          <el-table-column label="承保金额阈值" align="center" prop="underwritingAmountThreshold"/>
          <el-table-column  label="承包周期阈值" align="center" prop="contractPeriodThreshold"/>
          <el-table-column label="理赔金额阈值" align="center" prop="claimAmountThreshold"/>
          <el-table-column label="创建时间" align="center" prop="createTime"/>
          <el-table-column label="修改时间" align="center" prop="updateTime"/>
          <el-table-column label="创建人" align="center" prop="createBy"/>
          <el-table-column label="修改人" align="center" prop="updateBy"/>
          <el-table-column label="是否删除"
                           align="center"
                           prop="deleted"
                           :formatter="deletedFormat"
          />
          <el-table-column width="200" label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
              >修改
              </el-button>
<!--              <el-button-->
<!--                  size="mini"-->
<!--                  type="text"-->
<!--                  icon="el-icon-delete"-->
<!--                  @click="handleDelete(scope.row)"-->
<!--              >删除-->
<!--              </el-button>-->

            </template>
          </el-table-column>
        </el-table>
      </template>

      <template slot="pageDisplay">
        <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
        />
      </template>
    </list-page-container>


    <el-dialog :title="title" :visible.sync="open" width="530px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">


        <el-form-item label="承保金额阈值" prop="underwritingAmountThreshold">
          <el-input v-model="form.underwritingAmountThreshold" placeholder="请输入承保金额阈值"/>
        </el-form-item>
        <el-form-item label="承包周期阈值" prop="contractPeriodThreshold">
          <el-input v-model="form.contractPeriodThreshold" placeholder="请输入承包周期阈值"/>
        </el-form-item>
        <el-form-item label="理赔金额阈值" prop="claimAmountThreshold">
          <el-input v-model="form.claimAmountThreshold" placeholder="请输入理赔金额阈值"/>
        </el-form-item>



      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog
        title=""
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-upload
          class="upload-demo"
          :action="uploadAction"
          :on-preview="handlePreview"
          :on-remove="handleRemove"
          :before-remove="beforeRemove"
          multiple
          :limit="1"
          :on-exceed="handleExceed"
          :on-success="importData"
          :headers="{token}"
          :file-list="fileList">
        <el-button size="small" type="primary">点击上传</el-button>

      </el-upload>
      <!--      <span slot="footer" class="dialog-footer">-->
      <!--    <el-button @click="dialogVisible = false">取 消</el-button>-->
      <!--    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>-->
      <!--  </span>-->
    </el-dialog>


  </div>
</template>

<script>

import {
  getClaimsModelList,
  addClaimsModel,
  updateClaimsModel,
  delClaimsModel,
  getClaimsModelById,
  uploadUrl,
  importClaimsModel,

} from "@/api/precisionCompensate";
import {getConfigList,updateConfigOption,getConfigById} from "@/api/systemConfigOption"
import { getToken } from '@/utils/auth'
import {listDept} from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import {OPERATE_TYPE} from "@/constantData";

export default {
  dicts: ['enum_user_state', 'enum_user_type'],
  components:{Treeselect},
  name: "SystemConfigOption",
  data() {
    return {
      token:'',
      uploadAction : process.env.VUE_APP_BASE_API +uploadUrl,
      plantOptions: [
        {
          value: '10202',
          label: '旱地玉米'
        }, {
          value: '104',
          label: '大豆'
        }, {
          value: '104',
          label: '大豆收入'
        }, {
          value: '103',
          label: '水稻'
        }, {
          value: '10201',
          label: '水地玉米'
        },{
          value: '10202',
          label: '玉米期货'
        },{
          value: '104',
          label: '大豆期货'
        },{
          value: '181',
          label: '水稻制种'
        }],  //作物下拉列表
      damageOptions:[
        {
          value: '未受灾',
          label: '未受灾'
        } ,
        {
          value: '轻度受灾',
          label: '轻度受灾'
        } ,
        {
          value: '中度受灾',
          label: '中度受灾'
        } ,
        {
          value: '重度受灾',
          label: '重度受灾'
        }

      ],  //受灾程度下拉列表
      //条目id
      itemId: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
      },
      dataList: [],
      total: 0,
      loading: false,
      multiple: true,
      open: false,
      open2: false,
      // 弹出层标题
      title: "",
      form: {},
      baseForm: {
        underwritingAmountThreshold: null,
        contractPeriodThreshold: null,
        claimAmountThreshold: null,

      },
      // 表单校验
      rules: {
        underwritingAmountThreshold: [
          {required: true, message: "承保金额阈值不能为空", trigger: "blur"}
        ],
        contractPeriodThreshold: [
          {required: true, message: "承包周期阈值不能为空", trigger: "blur"}
        ],
        claimAmountThreshold: [
          {required: true, message: "理赔金额阈值不能为空", trigger: "blur"}
        ],

      },

      // 选中数组
      ids: [],
      deptOptions:[],
      hanType:'',
      OPERATE_TYPE,

      dialogVisible: false,  //导入弹框
      fileList:[],//导入数据
      fileId:null,  //文件id

    }
  },

  mounted(){
    this.token = getToken();
  },

  methods: {
    //映射是否删除
    deletedFormat(row){

      if(row.deleted === '1'){
        return '是';
      }else if(row.deleted === '0'){
        return '否'
      }else{
        return null
      }

    },
    //打开导入
    openImport(){
      this.fileList=[];
      this.dialogVisible=true;

    },
    //上传文件成功回调
    importData(res){
      if(res && res.code==200){
        if(res.data && res.data.id){
          this.fileId = res.data.id;
          this.sendFileId(res.data.id);
        }
      }
    },
    sendFileId(id){
      importClaimsModel({id}).then(res=>{
        if(res.code==200){
          this.dialogVisible=false;//关闭弹窗
          this.$message.success('导入成功')
          this.fileList=[];
        }else{
          this.dialogVisible=false;//关闭弹窗
          this.$message.error('导入失败');
          this.fileList=[];
        }
      },err=>{
        this.dialogVisible=false;//关闭弹窗
        this.fileList=[];
      })
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const dictId = row.id
      this.hanType = OPERATE_TYPE.UPDATE
      getConfigById({id:dictId}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改阈值";
      });
    },
    /** 导出按钮操作 */
    handleExport() {

    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.multiple = !selection.length
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.id || this.ids.join(',');
      this.$modal.confirm('是否确认删除当前数据项？').then(function () {
        return delClaimsModel({ids:dictIds});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateConfigOption(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addClaimsModel(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    // 表单重置
    reset() {
      this.form = _.cloneDeep(this.baseForm);
      this.resetForm("form");
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.hanType = OPERATE_TYPE.ADD
      this.open = true;
      this.title = "添加农户";
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getList() {
      this.loading = true;
      getConfigList(this.queryParams).then(response => {
            this.dataList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
      );
    },
    getDeptList(){
      let owner = this
      listDept().then(response => {
        owner.deptOptions = this.handleTree(response.data, "orgCode", 'parentCode');
      });
    }
  },
  created() {
    this.getList();
    this.getDeptList()
  },
}
</script>

<style scoped>

</style>
